﻿using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.UserLogics
{
    public class UserLogicsBackFillHandler : IRequestHandler<UserLogicsBackFillCommand, Result>
    {
        private readonly IBaseRepository<User>? _usersRepository;

        public UserLogicsBackFillHandler(IBaseRepository<User>? usersRepository)
        {
            _usersRepository = usersRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(UserLogicsBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var users = await (await _usersRepository.GetAll()).Where(c => c.UserId == request.UserId).FirstOrDefaultAsync();
                if (users != null)
                {
                    result.Code= (int)CommonErrorCode.Success;
                    result.Data = users;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        
    }
}
